package JFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

// 注册界面
public class RegisterJFrame extends JFrame implements ActionListener {
    JButton btnConfirm = new JButton("确认");
    JButton btnClear = new JButton("清除");
    JLabel labelUserName = new JLabel("注册名称");
    JTextField tfdUserName = new JTextField("长度不超过10个字符",12);
    JLabel labelPassword = new JLabel("注册密码");
    JTextField tfdPassword = new JTextField("长度不超过20个字符",12);
    JLabel labelTelephone = new JLabel("常用电话");
    JTextField tfdTelephone = new JTextField("长度固定11个字符",12);
    JLabel labelID = new JLabel("身份证号");
    JTextField tfdID = new JTextField("长度固定18个字符",12);
    JLabel labelName = new JLabel("真实姓名");
    JTextField tfdName = new JTextField("长度不超过15个字符",12);
    JLabel labelCardNumber = new JLabel("银行卡号");
    JTextField tfdCardNumber = new JTextField("长度固定19个字符",12);

    // 注册界面设置
    public RegisterJFrame(){
        this.setName("村镇银行业务储蓄系统");
        this.setTitle("注册界面");
        this.setSize(300, 400);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setLayout(new FlowLayout());

        // 用户名长度提示信息
        tfdUserName.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tfdUserName.setText("");
            }
        });

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (tfdUserName.getText().length() == 0)
                    tfdUserName.setText("长度不超过10个字符");
            }
        });

        // 密码长度提示信息
        tfdPassword.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tfdPassword.setText("");
            }
        });

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (tfdPassword.getText().length() == 0)
                    tfdPassword.setText("长度不超过20位");
            }
        });

        // 电话号码长度提示
        tfdTelephone.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tfdTelephone.setText("");
            }
        });

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (tfdTelephone.getText().length() == 0)
                    tfdTelephone.setText("长度固定11位");
            }
        });

        // 身份证号长度提示
        tfdID.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tfdID.setText("");
            }
        });

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (tfdID.getText().length() == 0)
                    tfdID.setText("长度固定18位");
            }
        });

        // 真实姓名长度提示
        tfdName.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tfdName.setText("");
            }
        });

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (tfdName.getText().length() == 0)
                    tfdName.setText("长度不超过15个字符");
            }
        });

        // 银行卡号长度提示
        tfdCardNumber.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tfdCardNumber.setText("");
            }
        });

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (tfdCardNumber.getText().length() == 0)
                    tfdCardNumber.setText("长度固定19位");
            }
        });

        JPanel p1 = new JPanel();
        p1.add(labelUserName);
        p1.add(tfdUserName);

        JPanel p2 = new JPanel();
        p2.add(labelPassword);
        p2.add(tfdPassword);

        JPanel p3 = new JPanel();
        p3.add(labelTelephone);
        p3.add(tfdTelephone);

        JPanel p4 = new JPanel();
        p4.add(labelID);
        p4.add(tfdID);

        JPanel p5 = new JPanel();
        p5.add(labelName);
        p5.add(tfdName);

        JPanel p6 = new JPanel();
        p6.add(labelCardNumber);
        p6.add(tfdCardNumber);

        JPanel p7 = new JPanel();
        p7.add(btnConfirm);
        p7.add(btnClear);

        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        this.add(p6);
        this.add(p7);
        this.setVisible(true);
        btnConfirm.addActionListener(this);
        btnClear.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 注册
        if(e.getSource() == btnConfirm){
            try{
                // 添加用户
                addUser();
            }
            catch (Exception exception){
                System.out.println("注册时出现异常");
            }
            this.dispose();
        }
        // 清空
        else if(e.getSource() == btnClear){
            tfdUserName.setText("");
            tfdPassword.setText("");
            tfdTelephone.setText("");
            tfdID.setText("");
            tfdName.setText("");
            tfdCardNumber.setText("");
        }
    }

    public void addUser() throws Exception{
        // 获取文本框里的内容
        String userNameText = tfdUserName.getText();
        String passwordText = tfdPassword.getText();
        String telephoneText = tfdTelephone.getText();
        String IDText = tfdID.getText();
        String nameText = tfdName.getText();
        String cardNumberText = tfdCardNumber.getText();
        // 注册校验
        if(!checkUserName(userNameText) || !checkPassword(passwordText) ||
        !checkTelephone(telephoneText) || !checkID(IDText) || !checkName(nameText)
        || !checkCardNumber(cardNumberText)) {
            RegisterFailedJFrame failedJFrame = new RegisterFailedJFrame();
            return;
        }
        // 所有验证都通过后,用户信息加入到数据库
        Statement statement = null;
        ResultSet result = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank";
            String username = "root";
            String mypassword = "20222746";
            connection = DriverManager.getConnection(ur1, username, mypassword);
            statement = connection.createStatement();
            String sql = "insert into user values(?,?,?,?,?,?,0,0)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userNameText);
            preparedStatement.setString(2, passwordText);
            preparedStatement.setString(3, telephoneText);
            preparedStatement.setString(4, IDText);
            preparedStatement.setString(5, nameText);
            preparedStatement.setString(6, cardNumberText);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    // 用户名长度验证
    public boolean checkUserName(String userName) {
        if(0 < userName.length() && userName.length() <= 10)
            return true;
        else
            return false;
    }

    // 密码长度验证
    public boolean checkPassword(String password) {
        if(0 < password.length() && password.length() <= 20)
            return true;
        else
            return false;
    }

    // 电话长度验证
    public boolean checkTelephone(String telephone) {
        if(telephone.length() == 11)
            return true;
        else
            return false;
    }

    // 身份证号长度验证
    public boolean checkID(String ID) {
        if(ID.length() == 18)
            return true;
        else
            return false;
    }

    // 真实姓名长度验证
    public boolean checkName(String name) {
        if(0 < name.length() && name.length() <= 15)
            return true;
        else
            return false;
    }

    // 银行卡号长度验证
    public boolean checkCardNumber(String cardNumber) {
        if(cardNumber.length() == 19)
            return true;
        else
            return false;
    }
}